<?php
/**
 * 代码solution管理
 * User: cqfut
 * Date: 2020/2/15
 * Time: 17:22
 */
require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."front/model/solution_model.php");

class SolutionRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function getSolutionInfo() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"您还未登录" );
            return;
        }

        $solutionId = $this->getReqIntVal('solution_id');
        if ( $solutionId <= 0 ) {
            $this->setRspStatus(0, -1,"参数错误" );
            return;
        }

        // 获取 solution信息
        $model = new SolutionModel();
        $info = $model->getSolutionInfo($solutionId, $userId);
        if ( $info == null ) {
            $this->setRspStatus(0, -2,"未查询到代码信息");
            return;
        }

        // 返回 solution 信息
        $this->setRspData($info);
    }

    function rejudgeSolution() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"您还未登录" );
            return;
        }

        // 获取参数 solution id
        $solutionId = $this->getReqIntVal('solution_id');
        if ( $solutionId <= 0 ) {
            $this->setRspStatus(0, -1,"参数错误" );
            return;
        }

        // 获取参数source code, 若 sourceCode 为空，则将 sourceCode 设 null
        $sourceCode = $this->getReqStrVal('source_code');
        if ( $sourceCode == null || $sourceCode == '' ) {
            $sourceCode = null;
        }

        $sourceCode = base64_decode($sourceCode);

        // 是否启用 O2 优化
        $enableO2 = $this->getReqIntVal('enable_o2');

        // 重新评测 solution
        $model = new SolutionModel();

        // 校验用户提交的代码
        if ( $sourceCode != null ) {
            $errMsg = $model->verifySourceCode($sourceCode);
            if ( $errMsg != null ) {
                $this->setRspStatus(0, -4,$errMsg);
                return;
            }
        }

        // Note: 添加提交时间限制

        // 提交代码，进行重新评测
        $ret = $model->rejudgeSolution($solutionId, $sourceCode, $enableO2, $userId);
        if ( $ret < 0 ) {
            $this->setRspStatus(0, -2,"提交评测代码失败");
            return;
        }
        $this->setRspStatus(1, 0,"提交评测代码成功" );
    }

    function queryJudgeStatus() {
        $userId = BaseRouter::getSession('user_id');
        if ( $userId == null ) {
            $this->setRspStatus(0, -1,"您还未登录" );
            return;
        }

        $solutionId = $this->getReqIntVal('solution_id');
        if ( $solutionId <= 0 ) {
            $this->setRspStatus(0, -1,"参数错误" );
            return;
        }

        // 获取 solution信息
        $model = new SolutionModel();
        $judgeStatus = $model->getJudgeStatus($solutionId);
        if ( $judgeStatus == null ) {
            $this->setRspStatus(0, -2,"未查询到代码信息");
            return;
        }

        // 返回 solution 评测状态
        $this->setRspData($judgeStatus);
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'rejudge' ) {
            $this->rejudgeSolution();
        }
        else if ( $op == 'fetch' ) {
            $this->getSolutionInfo();
        }
        else if ( $op == 'query_status') {
            $this->queryJudgeStatus();
        }
    }
}

$router = new SolutionRouter();
$privilegeNames = array('administrator', 'teacher');
if ( ! $router->checkAuth($privilegeNames) ) {
    $router->setRspStatus(0, -1001,"不具备该权限");
    $router->finish();
    exit(1);
}
$router->init();
$router->run();
$router->finish();
